package myshare;

import java.sql.SQLException;
import java.util.List;

import jdbc.JDBCUtils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import mybeans.Beans;
import mybeans.Page;

public class MyShareService {

	@SuppressWarnings("deprecation")
	public Page getPage(String userID) {
		
		QueryRunner qr = new QueryRunner(JDBCUtils.getDs());
		String sql = "select count(*) as sum from Share where userID = ?";
		
		
		try {
			return qr.query(sql, userID, new BeanHandler<>(Page.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch b
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("deprecation")
	public List<Beans> getAPageShare(String userID, Page pageBean) {
		
		
		int c=pageBean.getSum();
		int countpage1=c-pageBean.getNuminpage()*(pageBean.getCurrentpage()-1);
		String count=Integer.toString(countpage1);
		
		System.out.println(count);
		QueryRunner qr = new QueryRunner(JDBCUtils.getDs());
		String sql = "select top "+pageBean.getNuminpage()+"* from (select top "+count+"* from Share where userID = ? order by path desc)g order by path asc";
		
		
		try {
			return qr.query(sql, userID,  new BeanListHandler<>(Beans.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}

}
